package com.eascs.spider.dsk;

import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.eascs.spider.dsk.db.RedisSession;
import com.eascs.spider.dsk.entity.QueryForm;
import com.eascs.spider.dsk.entity.StaticParam;
import com.eascs.spider.dsk.spider.DskCompanyDetailSpider;
import com.eascs.spider.dsk.spider.DskCompanyListSpider;

/**
 * 鼎搜客爬虫启动类
 *
 */
@SpringBootApplication
public class Main {
	private static Logger logger = LoggerFactory.getLogger(Main.class);
	
    public static void main( String[] args ){
    	startCrawlDetail();
    	startCrawlCompanyList();
    }
    
    //启动详情采集
    public static void startCrawlDetail(){
    	ExecutorService executors = Executors.newFixedThreadPool(5);
    	Runnable spider = new DskCompanyDetailSpider();
    	executors.execute(spider);
    }
    
    //启动公司列表采集
    public static void startCrawlCompanyList(){
    	RedisSession session = RedisSession.getIntance();
		QueryForm qr = QueryForm.getInstance();
		ExecutorService executors = Executors.newFixedThreadPool(5);
		
		//1，如果检测是第一次启动，则加载所有任务条件到redis 集合dsk_queryParam_cache
		if(session.hkeys(StaticParam.DSK_REDIS_QUERYPARAM_CACHE).size() == 0){
			while(true){
				JSONObject param = qr.getQueryFormJson();
				if(param == null)
					break;
				session.hset(StaticParam.DSK_REDIS_QUERYPARAM_CACHE, param.getString("Brand")+"|"+param.getString("Channel"), 0+"");
			}
		}
		//2，加载缓存，从上次停止的地方开始，从redis集合dsk_queryParam_cache取出所有任务条件key
		//2.1，构造一个模子的param
		Set<String> set = session.hkeys(StaticParam.DSK_REDIS_QUERYPARAM_CACHE);
		JSONObject param = qr.getQueryFormJsonModel();
		
		for(String key : set){
			String value = session.hget(StaticParam.DSK_REDIS_QUERYPARAM_CACHE, key);
			String brand = key.split("\\|")[0];
			String channel = key.split("\\|")[1];
			param.put("Brand", brand);
			param.put("Channel", channel);
			if(StaticParam.TASK_REDIS_FINISHED.equals(value)){
				logger.info("采集完成的任务参数:{}" , key);
				continue;
			}else if("0".equals(value)){
				logger.info("采集次数为零的任务参数:{}" , key);
				param.put("pageIndex", ""+1);
			}else{
				int pageIndex = Integer.valueOf(session.hget(StaticParam.DSK_REDIS_QUERYPARAM_CACHE, key));
				logger.info("未完成的任务参数:{} ，翻页从第{}页开始" ,key ,pageIndex+"");
				param.put("pageIndex", pageIndex+1+"");
			}
			Runnable spider = new DskCompanyListSpider(param);
			executors.execute(spider);
		}    	
    }
}
